package com.server.object;

import java.util.ArrayList;
import java.util.List;

import com.server.app.PlayerManager;
import com.server.app.RoomManager;
import com.server.response.LoadedGameResp;
import com.server.response.OtherJoinResp;
import com.server.response.OtherLeaveResp;
import com.server.response.ReBroadcastResp;
import com.server.response.ReadyGameResp;
import com.server.status.SocketState;
import com.server.tools.RedeemCode;
import com.server.tools.JsonUtil;

public class Room {
	
	/**
	 * appid
	 */
	public long appid;
	
	/**
	 * 房间id
	 */
	public String room_id;
	
	/**
	 * 房主id
	 */
	public long master_id;
	
	/**
	 * 房间创建者（房主）uid
	 */
	public long creator;
	
	/**
	 * 玩家数量
	 */
	public long player_count;
	
	/**
	 * 玩家列表 player[0]默认为房主
	 */
	public List<Long> players;
	
	/**
	 * 房间标签
	 */
	public String tag;
	
	/**
	 * 房间状态
	 */
	public SocketState room_state;
	
	/**
	 * 准备列表
	 */
	public List<Long> ready_list = new ArrayList<Long>();
	
	/**
	 * 加载列表
	 */
	public List<Long> loaded_list = new ArrayList<Long>();
	
	/**
	 * 实例化房间
	 * @param player_count 最大玩家数量
	 * @param tag 房间标签
	 */
	public Room(int player_count, String tag, long creator, long appid) {
		this.appid = appid;
		this.tag = tag;
		this.player_count = player_count;
		players = new ArrayList<Long>();
		room_id = RedeemCode.create((byte)1,1,12,"roomid")[0];
		this.creator = creator;
		room_state = SocketState.IDLE;
		master_id = creator;
		players.add(creator);
	}
	
	/**
	 * 添加玩家
	 * @param uid
	 * @return
	 */
	public SocketState AddPlayer(long uid) {
		if(room_state == SocketState.IDLE) {
			if(players.size() < player_count) {
				players.add(uid);
				PlayerData pd = new PlayerData();
				pd.uid = uid;
				pd.data = PlayerManager.GetInstance().GetPlayer(uid).data;
				String data = JsonUtil.ObjectToJsonString(pd);
				for(int i = 0; i < players.size(); i++) {
					if(uid != players.get(i)) {
						Player player = PlayerManager.GetInstance().GetPlayer(players.get(i));
						if(player != null) {
							OtherJoinResp resp = new OtherJoinResp(data);
							resp.setUserInfo(player.userInfo);
							resp.Send();
						}
					}
				}
				return SocketState.SUCCESSED;
			}else {
				return SocketState.FULLPLAYER;
			}
		}else {
			return SocketState.ONGAMING;
		}
	}
	
	/**
	 * 移除玩家
	 * @param uid 要移除的玩家id
	 */
	public void RemovePlayer(long uid) {
		for(int i = 0; i < players.size(); i++) {
			if(players.get(i) == uid) {
				players.remove(i);
				if(players.isEmpty()) {
					RoomManager.GetInstance().RemoveRoom(appid, tag, room_id, this);
				}else {
					if(uid == master_id)master_id = players.get(0);
					for(int j = 0; j < ready_list.size(); j++) {
						if(ready_list.get(j) == uid) {
							ready_list.remove(j);
							break;
						}
					}
					
					for(int j = 0; j < loaded_list.size(); j++) {
						if(loaded_list.get(j) == uid) {
							loaded_list.remove(j);
							break;
						}
					}
					
					for(int j = 0; j < players.size(); j++) {
						Player player = PlayerManager.GetInstance().GetPlayer(players.get(j));
						if(player!=null) {
							OtherLeaveResp resp = new OtherLeaveResp(uid, master_id);
							resp.setUserInfo(player.userInfo);
							resp.Send();
						}
					}
				}
				return;
			}
		}
	}
	
	/**
	 * 准备游戏
	 * @param uid 玩家uid
	 * @return
	 */
	public List<Long> ReadyGame(long uid) {
		if(ready_list.indexOf(uid) == -1) {
			ready_list.add(uid);
			for(int i = 0; i < players.size(); i++) {
				if(uid != players.get(i)) {
					Player player = PlayerManager.GetInstance().GetPlayer(players.get(i));
					if(player!=null) {
						ReadyGameResp resp = new ReadyGameResp(SocketState.SUCCESSED.ordinal(), ready_list);
						resp.setUserInfo(player.userInfo);
						resp.Send();
					}
				}
			}
			if(ready_list.size() >= player_count)room_state = SocketState.LOADING;
		}
		return ready_list;
	}
	
	/**
	 * 加载游戏完成
	 * @param uid 玩家uid
	 * @return
	 */
	public List<Long> LoadedGame(long uid) {
		if(loaded_list.indexOf(uid) == -1) {
			loaded_list.add(uid);
			for(int i = 0; i < players.size(); i++) {
				if(uid != players.get(i)) {
					Player player = PlayerManager.GetInstance().GetPlayer(players.get(i));
					if(player!=null) {
						LoadedGameResp resp = new LoadedGameResp(SocketState.SUCCESSED.ordinal(), loaded_list);
						resp.setUserInfo(player.userInfo);
						resp.Send();
					}
				}
			}

			if(loaded_list.size() >= player_count)room_state = SocketState.GAMING;
		}
		return loaded_list;
	}
	
	/**
	 * 获取玩家头像等数据
	 * @param uid 获取这的id，用于判断不返回
	 * @return 房间除获取者外所有人的数据
	 */
	public List<PlayerData> GetPlayerData(long uid) {
		List<PlayerData> pdl = new ArrayList<PlayerData>();
		try {
			for(int i = 0; i < players.size(); i++) {
				if(players.get(i) != uid) {
					Player player = PlayerManager.GetInstance().GetPlayer(players.get(i));
					PlayerData pd = new PlayerData();
					pd.uid = player.uid;
					pd.data = player.data;
					pd.isReady = ready_list.indexOf(player.uid) == -1 ? (byte)0: (byte)1;
					if(player != null) {
						pdl.add(pd);
					}
				}
			}
		}catch(Exception  e){
			e.printStackTrace();
			System.out.println(JsonUtil.ObjectToJsonString(players));
		}
		
		return pdl;
	}
	
	/**
	 * 广播
	 * @param uid
	 */
	public void Broadcast(long uid, String data, String key, boolean isReturn) {
		for(int i = 0; i < players.size(); i++) {
			if(players.get(i) != uid || isReturn) {
				Player player = PlayerManager.GetInstance().GetPlayer(players.get(i));
				if(player != null) {
					ReBroadcastResp resp = new ReBroadcastResp(data, uid, key);
					resp.setUserInfo(player.userInfo);
					resp.Send();
				}
			}
		}
	}
}
